﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using RaisingForce.Raise.Measures;
using RaisingForce.Raise.ObjectModel;

namespace RaisingForce.Raise.Presentation.ViewModel.Specialized
{
    public static class MeasureViewModelHelper
    {
        #region DistanceMeasures Static Property

        public static IDoubleMeasureViewModelFactory<IDistanceMeasure> DistanceMeasures
        {
            get
            {
                Contract.Ensures(Contract.Result<IDoubleMeasureViewModelFactory<IDistanceMeasure>>() != null);
                return DoubleMeasureViewModelFactory<IDistanceMeasure>.Instance;
            }
        }

        #endregion

        #region TimeMeasures Static Property

        public static IDoubleMeasureViewModelFactory<ITimeMeasure> TimeMeasures
        {
            get
            {
                Contract.Ensures(Contract.Result<IDoubleMeasureViewModelFactory<ITimeMeasure>>() != null);
                return DoubleMeasureViewModelFactory<ITimeMeasure>.Instance;
            }
        }

        #endregion

        #region AngleMeasures Static Property

        public static IDoubleMeasureViewModelFactory<IAngleMeasure> AngleMeasures
        {
            get
            {
                Contract.Ensures(Contract.Result<IDoubleMeasureViewModelFactory<IAngleMeasure>>() != null);
                return DoubleMeasureViewModelFactory<IAngleMeasure>.Instance;
            }
        }

        #endregion

        #region UpdateLocalization Public Static Method

        public static void UpdateLocalization()
        {
            DistanceMeasures.UpdateLocalization();
            TimeMeasures.UpdateLocalization();
            AngleMeasures.UpdateLocalization();
        }

        #endregion

        #region Measure Extension Methods

        public static DoubleMeasureViewModel<T> ToViewModel<T>(
            this T measure,
            IDoubleMeasureViewModelFactory<T> viewModelFactory)
            where T : IMeasure<double>
        {
            if (measure == null)
            {
                throw new ArgumentNullException("measure");
            }
            if (viewModelFactory == null)
            {
                throw new ArgumentNullException("viewModelFactory");
            }
            Contract.Ensures(Contract.Result<DoubleMeasureViewModel<T>>() != null);
            Contract.EndContractBlock();
            return viewModelFactory.CreateViewModel(measure);
        }

        public static DoubleMeasureViewModel<IDistanceMeasure> ToViewModel(
            this IDistanceMeasure measure)
        {
            Contract.Requires(measure != null);
            return ToViewModel(measure, DistanceMeasures);
        }

        public static DoubleMeasureViewModel<ITimeMeasure> ToViewModel(
            this ITimeMeasure measure)
        {
            Contract.Requires(measure != null);
            return ToViewModel(measure, TimeMeasures);
        }

        public static DoubleMeasureViewModel<IAngleMeasure> ToViewModel(
            this IAngleMeasure measure)
        {
            Contract.Requires(measure != null);
            return ToViewModel(measure, AngleMeasures);
        }

        #endregion

        #region Enumerable Extension Methods

        public static IEnumerable<DoubleMeasureViewModel<T>> ToViewModels<T>(
            this IEnumerable<T> measures,
            IDoubleMeasureViewModelFactory<T> viewModelFactory)
            where T : IMeasure<double>
        {
            if (measures == null)
            {
                throw new ArgumentNullException("measures");
            }
            if (viewModelFactory == null)
            {
                throw new ArgumentNullException("viewModelFactory");
            }
            Contract.Ensures(Contract.Result<IEnumerable<DoubleMeasureViewModel<T>>>() != null);
            Contract.EndContractBlock();
            return measures.Select(measure =>
                viewModelFactory.CreateViewModel(measure));
        }

        public static IEnumerable<DoubleMeasureViewModel<IDistanceMeasure>> ToViewModels(
            this IEnumerable<IDistanceMeasure> measures)
        {
            Contract.Requires(measures != null);
            Contract.Ensures(Contract.Result<IEnumerable<DoubleMeasureViewModel<IDistanceMeasure>>>() != null);
            return ToViewModels(measures, DistanceMeasures);
        }

        public static IEnumerable<DoubleMeasureViewModel<ITimeMeasure>> ToViewModels(
            this IEnumerable<ITimeMeasure> measures)
        {
            Contract.Requires(measures != null);
            Contract.Ensures(Contract.Result<IEnumerable<DoubleMeasureViewModel<ITimeMeasure>>>() != null);
            return ToViewModels(measures, TimeMeasures);
        }

        public static IEnumerable<DoubleMeasureViewModel<IAngleMeasure>> ToViewModels(
            this IEnumerable<IAngleMeasure> measures)
        {
            Contract.Requires(measures != null);
            Contract.Ensures(Contract.Result<IEnumerable<DoubleMeasureViewModel<IAngleMeasure>>>() != null);
            return ToViewModels(measures, AngleMeasures);
        }

        #endregion
    }
}
